package com.ohuang.jiaxiaotong.Activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.myhttp.CallBackString;
import com.example.myhttp.Ihttp;
import com.ohuang.jiaxiaotong.MyHttpURL;
import com.ohuang.jiaxiaotong.R;
import com.ohuang.jiaxiaotong.Util.Ohuang;
import com.ohuang.jiaxiaotong.Util.Sys;
import com.ohuang.jiaxiaotong.base.BaseAc;

import java.util.HashMap;
import java.util.Objects;

public class RegisterActivity extends BaseAc {
    private RadioGroup radioGroup;
    private int role = 0;
    private TextView textView;
    private String shenfeng[] = {"老师", "家长"};
    private EditText editText_username, editText_psd1, editText_psd2;
    private String username, psd1, psd2;
    private static final int RE_SUCCESS = 0, RE_FAIL = 1, RE_ERROR = 2, RE_OTHER = 3;
    private Dialog dialogLoading;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        setTitle("注册");
        Ohuang.changeStatusBarTextImgColor(this, true);
        Objects.requireNonNull(getSupportActionBar()).setHomeAsUpIndicator(R.mipmap.icon_goback);//设置ActionBar返回按钮

        initview();


    }

    private void initview() {
        editText_username = findViewById(R.id.edit_username_register);
        editText_psd1 = findViewById(R.id.edit_psd1_register);
        editText_psd2 = findViewById(R.id.edit_psd2_register);

        radioGroup = findViewById(R.id.rg_register);
        textView = findViewById(R.id.txt_role_register);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_parent_register:
                        role = 1;
                        break;
                    case R.id.rb_teacher_register:
                        role = 0;
                        break;
                }
                textView.setText(shenfeng[role]);
            }
        });
        textView.setText(shenfeng[role]);
    }


    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        if (item.getItemId() == android.R.id.home) {//左上角返回按钮
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    //注册按钮点击事件
    public void RegisterClick(View view) {
        if (Sys.isClick()) {
            username = editText_username.getText().toString();
            psd1 = editText_psd1.getText().toString();
            psd2 = editText_psd2.getText().toString();
            if (username.isEmpty() || psd2.isEmpty() || psd1.isEmpty()) {
                Ohuang.dialogshow(RegisterActivity.this, "注册失败", "用户名和密码不能为空");
                return;
            }
            if (!psd1.equals(psd2)) {
                Ohuang.dialogshow(RegisterActivity.this, "注册失败", "两次密码输入不一致");
                return;
            }
            dialogLoading = Ohuang.dialogLoadingshow(this, "正在注册");
            httpRegister();//注册

        }
    }


    //注册请求
    private void httpRegister() {
        HashMap<String, String> map = new HashMap<>();
        map.put("username", username);
        map.put("password", psd1);
        map.put("role", role + "");
        Ihttp.getInstance().post(MyHttpURL.mainUrl + "user/register", map, new CallBackString() {
                    @Override
                    public void success(String ojb) {
                        String code = ojb;
                        if ("success".equals(code)) {
                            myHandler.sendEmptyMessage(RE_SUCCESS);
                        } else if ("error".equals(code)) {
                            myHandler.sendEmptyMessage(RE_FAIL);
                        } else {
                            myHandler.sendEmptyMessage(RE_OTHER);
                        }
                    }

                    @Override
                    public void fail(String s) {
                        myHandler.sendEmptyMessage(RE_ERROR);
                    }
                }
        );


    }

    @SuppressLint("HandlerLeak")
    private Handler myHandler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case RE_SUCCESS:
                    dialogLoading.dismiss();
                    Ohuang.dialogshow(RegisterActivity.this, "注册成功", "注册成功,你已经可以登录该账号了", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            finish();
                        }
                    });
                    break;
                case RE_FAIL:
                    dialogLoading.dismiss();
                    Ohuang.dialogshow(RegisterActivity.this, "注册失败", "注册失败,该用户名可能已被注册");
                    break;
                case RE_ERROR:
                    dialogLoading.dismiss();
                    Ohuang.dialogshow(RegisterActivity.this, "注册失败", "网络连接错误");
                    break;
                case RE_OTHER:
                    dialogLoading.dismiss();
                    Ohuang.dialogshow(RegisterActivity.this, "注册失败", "返回值错误");
                    break;

            }
        }
    };
}
